/**
 * 项目名称: Topic_938.cpp
 * 项目路径: study/DataStructure/LeetCode/树/简单
 * 编辑时间: 2024年08月23日08时23分24秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode() : val(0), left(nullptr), right(nullptr) {}

    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int ans = 0;
    void pre(TreeNode * root,int low,int high){
        if(root){
            if(root->val>=low && root->val<=high)ans+=root->val;
//            if(root->left->val<low || root->left->val >high)return;
//            if(root->right->val<low || root->right->val >high)return;
            pre(root->left,low,high);
            pre(root->right,low,high);
        }
    }
    int rangeSumBST(TreeNode* root, int low, int high) {
        if(!root)return ans;
        pre(root,low,high);
        return ans;
    }
};
